package com.jjlin.leetcode;
import java.util.*;

public class Solution20 {
    public boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        int len = s.length();
        for(int i = 0; i < len; i++){
            char ch = s.charAt(i);
            if(ch == '(' || ch == '{' || ch == '[')//左括号
                stack.push(ch);
            else if(ch == ')'){//检查右括号
                if(stack.isEmpty() || stack.pop() != '(')
                    return false;
            } else if(ch == ']'){
                if(stack.isEmpty() || stack.pop() != '[')
                    return false;
            } else if(ch == '}'){
                if(stack.isEmpty() || stack.pop() != '{')
                    return false;
            }
        }
        return stack.isEmpty();
    }
}
